package com.bkgy.modular.tbmodelset.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.excelhandle.PlanExcelHandlerImpl;
import com.bkgy.modular.tbmodelset.dao.TbModelSet2130Mapper;
import com.bkgy.modular.tbmodelset.dao.TbModelSetDao;
import com.bkgy.modular.tbmodelset.entity.*;
import com.bkgy.modular.tbmodelset.service.TbModelSetService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.CodiUtil;
import com.bkgy.util.ExcelUtilsPOI;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 轧制仿真设定值接口具体实现类
* @author  生成器生成
* @date 2022-10-18 13:37:23
*/
@Service
public class TbModelSetServiceImpl  extends ServiceImpl<TbModelSetDao, TbModelSet> implements TbModelSetService {
	
	@Autowired
	private TbModelSetDao tbModelSetDao;

	@Autowired
	private TbModelSet2130Mapper tbModelSet2130Mapper;
	
	/**
     * 添加记录
     * @param data json字符串
     * @param userId 用户id
     * @param sname 用户姓名
     */
	public ResultData insertDataTbModelSet(String data,Long userId,String sname){
		try{
			JTbModelSet jtbModelSet = JSON.parseObject(data,JTbModelSet.class);
    		TbModelSet tbModelSet = jtbModelSet.getTbModelSet();
    		CodiUtil.newRecord(userId,sname,tbModelSet);
            tbModelSetDao.insertDataTbModelSet(tbModelSet);
            return ResultData.ResultDataSuccessSelf("添加数据成功", null);
    	}catch(Exception e){
    		e.printStackTrace();
    		return ResultData.ResultDataFaultSelf("添加失败，错误信息为" + e.getMessage(), null); 
    	}
	};
	
	/**
	 * 根据主键删除对象
	 * @param indocno  对象主键
	 * */
	public ResultData deleteDataTbModelSetOne(Long indocno){
		try {
            tbModelSetDao.deleteDataTbModelSetOne(indocno);
             return ResultData.ResultDataSuccessSelf("删除数据成功", null);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null); 
        }
	};
	
	/**
     * 根据主键字符串删除多个对象
     * @param str_id 对象主键字符串
     */
    public ResultData deleteDataTbModelSetMany(String str_id) {
        try {
        	String sql = "delete tb_model_set where indocno in(" + str_id +")";
            tbModelSetDao.deleteDataTbModelSetMany(sql);
            return ResultData.ResultDataSuccessSelf("删除数据数据成功", null);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("删除失败,失败信息为" + e.getMessage(), null); 
        }
    }
	
	/**
     * 修改记录
     * @param userId 用户id
     * @param sname 用户姓名
     * @param data sysUser 对象实体
     */
	public ResultData updateDataTbModelSet(String data,Long userId,String sname){
		try {
			JTbModelSet jtbModelSet = JSON.parseObject(data,JTbModelSet.class);
    		TbModelSet tbModelSet = jtbModelSet.getTbModelSet();
        	CodiUtil.editRecord(userId,sname,tbModelSet);
            tbModelSetDao.updateDataTbModelSet(tbModelSet);
            return ResultData.ResultDataSuccessSelf("修改成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("修改失败,失败信息为" + e.getMessage(), null); 
        }
	};
	
	/**
     * 分页查看记录
     * @param jsonObject 分页参数字符串
     */
    public ResultData findDataTbModelSetByPage(JSONObject jsonObject) {
        try {
			Integer pageIndex = jsonObject.getInteger("pageIndex");
			Integer pageSize = jsonObject.getInteger("pageSize");
			if (pageIndex == null && pageSize == null) {
				return ResultData.ResultDataFaultSelf("检查分页参数是否正确",-1);
			}
			JSONObject condition = jsonObject.getJSONObject("condition");

			List<TbModelSet1720> list = tbModelSetDao.findDataTbModelSetByPage1720((pageIndex - 1) * pageSize, pageIndex * pageSize, condition);
    		Integer count = tbModelSetDao.findDataTbModelSetByPageCount(condition);
    		return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null); 
        }
    }
    
    /**
     * 根据主键查询单条记录
     * @param data 分页参数字符串
     */
    public ResultData findDataTbModelSetByIndocno(String data) {
        try {
        	JTbModelSet jtbModelSet = JSON.parseObject(data, JTbModelSet.class); 
        	Long indocno = jtbModelSet.getIndocno();
        	
    		TbModelSet tbModelSet = tbModelSetDao.findDataTbModelSetByIndocno(indocno);
    		return ResultData.ResultDataSuccess(tbModelSet);
        } catch (Exception e) {
        	e.printStackTrace();
        	return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null); 
        }
    }
	
	/**
	 * 查看记录
	 * @return list 对象集合返回
	 * */
	public List<TbModelSet> findDataTbModelSet(){
		List<TbModelSet> list = tbModelSetDao.findDataTbModelSet();
		return list;
	}

	@Override
	public ResultData findDataTbModelSetByPage2130(JSONObject jsonObject) {
		Integer pageIndex = jsonObject.getInteger("pageIndex");
		Integer pageSize = jsonObject.getInteger("pageSize");
		if (pageIndex == null && pageSize == null) {
			return ResultData.ResultDataFaultSelf("检查分页参数是否正确",-1);
		}
		JSONObject condition = jsonObject.getJSONObject("condition");

		List<TbModelSet2130> list = tbModelSet2130Mapper.findDataTbModelSetByPage((pageIndex - 1) * pageSize, pageIndex * pageSize, condition);
		Integer count = tbModelSet2130Mapper.findDataTbModelSetByPageCount(condition);
		return ResultData.ResultDataSuccess(list, count);
	}

	@Override
	public void exportTbModelSet(JSONObject jsonObject, HttpServletResponse response) {
		JSONObject condition = jsonObject.getJSONObject("condition");
		List<TbModelSet2130> listExcel = tbModelSet2130Mapper.findDataTbModelSetByNoPage(condition);

		List<ExportTbModelSet2130> list = new ArrayList<>();
		for (TbModelSet2130 item : listExcel) {
			ExportTbModelSet2130 exportTbModelSet2130 = new ExportTbModelSet2130();
			BeanUtils.copyProperties(item,exportTbModelSet2130);
			list.add(exportTbModelSet2130);
		}
		try {
			Map<String,Object> data1 = new HashMap();
			data1.put("data",list);
			File file = ResourceUtils.getFile("classpath:exceltemplate\\2130酸轧生产实绩.xlsx");
			TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(),false);
			templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
			Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data1);
			ExcelUtilsPOI.exportExcel("213o酸轧生产实际",response,workbook);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}



		//ExcelUtils.downloadExcel("213o酸轧生产实际",response,TbModelSet2130.class,list);
	}
}
